<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
  <meta charset="utf-8">
  <title>码值列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/adminui/dist/css/admin.css}" media="all">
  <link rel="stylesheet" th:href="@{/style/custom.css}" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <input type="hidden" id="dictType" name="dictType" th:value="${dictType}">
    <div class="layui-card-body">
      <div class="layui-btn-group" style="padding-bottom: 10px;">
        <!-- 新增 -->
        <button sec:authorize="hasAuthority('sys:dict:data:add')" type="button" class="layui-btn layui-btn-normal btn-op-dict-code layui-btn-sm" data-type="add">
          <i class="layui-icon">&#xe654;</i>新增
        </button>
        <button type="button" class="layui-btn layui-btn-normal btn-op-dict-code layui-btn-sm" data-type="refresh">
          <i class="layui-icon">&#xe666;</i>刷新
        </button>
      </div>

      <table id="LAY-dict-code-table" lay-filter="LAY-dict-code-table"></table>

      <script type="text/html" id="dict-code-table">
        <a sec:authorize="hasAuthority('sys:dict:data:edit')" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
        <a sec:authorize="hasAuthority('sys:dict:data:del')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>

      <script type="text/html" id="switchTpl">
        <input type="checkbox" disabled name="status" value="{{d.status}}" lay-skin="switch" lay-text="启用|禁用" {{ d.status== 'Y' ?'checked' : '' }}>
      </script>
    </div>
  </div>
</div>

<script th:src="@{/layui/layui.js}"></script>
<script>
  layui.config({
    base: '/' //  建议写绝对路径，相对路径很容易出问题
  }).extend({
    //设定模块别名，模块名是使用layui.模块名  模块文件是 js名字。不设置模块名 = 模块文件名
    dictCode: 'modules/sys/dict/code'
  });

  layui.use(['dictCode'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,form = layui.form
        ,table = layui.table;

    //事件
    let active = {
      add: function () {
        layer.open({
          type: 2
          , title: '新增'
          , content: '/sys/dict/code/turn/edit?dictType=' + layui.$("#dictType").val()
          // ,maxmin: true
          , area: ['500px', '540px']
          , resize: false
          , btn: ['确定', '取消']
          , yes: function (index, layero) {
            let iframeWindow = window['layui-layer-iframe' + index]
                , submitID = 'LAY-dict-code-front-submit'
                , submit = layero.find('iframe').contents().find('#' + submitID);

            //监听提交
            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
              let field = data.field; //获取提交的字段

              //提交 Ajax 成功后，静态更新表格中的数据
              admin.req({
                url: '/system/dict/code/save',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify(field),
                success: function (res) {
                  //执行成功
                  layer.msg(res.msg, {icon: 1});
                  table.reload('LAY-dict-code-table'); //数据刷新
                  layer.close(index); //关闭弹层
                }
              });
            });

            submit.trigger('click');
          }
        });
      }
      , refresh: function () {
        //执行重载 数据刷新
        table.reload('LAY-dict-code-table', {
          where: {
            dictType: $("#dictType").val()
          }
        });
      }
    };

    //执行事件操作
    $('.layui-btn.btn-op-dict-code').on('click', function () {
      let type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

  });
</script>
</body>
</html>
